/**
 * Created by cchen on 2015/6/8.
 */
'use strict';

angular.module('backofficeApp.bannerList.controllers', []).
    controller('bannerListCtrl', ['$scope', '$routeParams', 'CommunityService', '$http', '$location', function ($scope, $routeParams, communityService, $http, $location) {

        $scope.currentPage = 1;
        $scope.numPages = 999;



        $scope.bannerType = [
            {
                name: 'banner',
                value: 1
            },
            {
                name: '新鲜事',
                value: 2
            }
        ];

        $scope.value = Number($routeParams.type);

        $scope.getData = function () {
            if ($scope.value == null) {
                $scope.value = 3;
            }
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/getBannerList/' + $scope.value + '/' + $scope.currentPage, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.bannerList = data.data;
            }).error(function () {
            });
        }

        $scope.getData();

        $scope.clickAddBanner = function () {
            $location.path('/bannerAdd/'+$scope.value)
        };

        $scope.bannerEdit = function (bannerId) {
            $location.path('/bannerEdit/' + bannerId)
        }

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getData();
        };

    }]).directive('paging', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {

                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.numPages ? scope.numPages : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('numPages', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.numPages;
                };

            }
        };
    });